﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using PlutoStudio.Dependency;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;

namespace PlutoStudio.EF.UnitOfWork
{
    public class ActiveTransactionInfo
    {
        private readonly DbContext _starterDbContext;
        private readonly List<DbContext> _attendedDbContexts;

        public IDbContextTransaction DbContextTransaction { get; }

        public DbContext StarterDbContext => _starterDbContext;

        public IEnumerable<DbContext> AttendedDbContexts => _attendedDbContexts.ToImmutableList();

        internal ActiveTransactionInfo(IDbContextTransaction dbContextTransaction, DbContext starterDbContext)
        {
            DbContextTransaction = dbContextTransaction;
            _starterDbContext = starterDbContext;

            _attendedDbContexts = new List<DbContext>();
        }

        internal void Add(DbContext dbContext)
        {
            _attendedDbContexts.Add(dbContext);
        }
    }
}
